using System;
using System.Linq;
using System.Windows;
using jSLCore.Client.Presentation.BaseControl.Selection;
using Telerik.Windows.Controls;


namespace jSLCore.Client.Presentation.Wpf.TelerikSupport.Selection
{
    public class jSLRadTree : RadTreeView,ISelectableGrid
    {
        public jSLRadTree()
        {
            base.SelectionChanged += (sender, args) => InvokeSelectionChanged();
            Checked += (o, eventArgs) =>
                                {
                                    if (SelectionMode == GridSelectionMode.SingleRow)
                                    {
                                        var frameworkElement = (FrameworkElement) eventArgs.OriginalSource;
                                        InvokeRowSelected(new RowSelectedEventArgs(frameworkElement.DataContext));
                                    }
                                };
        }

        public object[] GetSelectedRows()
        {
            return SelectedItems.ToArray();
        }

        public new GridSelectionMode SelectionMode
        {
            get
            {
                if(IsOptionElementsEnabled)
                {
                    if(ItemsOptionListType==OptionListType.CheckList)
                    {
                        return GridSelectionMode.MultiRow;
                    }
                    if(ItemsOptionListType==OptionListType.OptionList)
                    {
                        return GridSelectionMode.SingleRow;
                    }
                }
                return GridSelectionMode.None;
            }
            set
            {
                switch (value)
                {
                        case GridSelectionMode.None:
                            IsOptionElementsEnabled = false;
                            break;
                        case GridSelectionMode.SingleRow:
                        IsTriStateMode = false;
                            IsOptionElementsEnabled = true;
                            ItemsOptionListType=OptionListType.OptionList;
                        break;
                        case GridSelectionMode.MultiRow:
                            IsOptionElementsEnabled = true;
                            IsTriStateMode = true;
                            ItemsOptionListType = OptionListType.CheckList;
                        break;
                }
            }
        }

        public new event EventHandler SelectionChanged;

        public void InvokeSelectionChanged()
        {
            EventHandler handler = SelectionChanged;
            if (handler != null) handler(this, EventArgs.Empty);
        }

        public event EventHandler<RowSelectedEventArgs> RowSelected;

        public void InvokeRowSelected(RowSelectedEventArgs e)
        {
            EventHandler<RowSelectedEventArgs> handler = RowSelected;
            if (handler != null) handler(this, e);
        }
    }
}